/**
 * Author:   e20160504
 * Date:     2018/6/5 16:41
 * Description:
 */
package com.kevin.demo.controller.area;

import com.kevin.demo.entity.area.Area;
import com.kevin.demo.exception.MyException;
import com.kevin.demo.framwork.Result;
import com.kevin.demo.framwork.ResultUtils;
import com.kevin.demo.service.area.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/area")
@Api(value = "AreaController",description = "区域controller")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @PostMapping
    @ApiOperation(value = "新增")
    public Result insert(@RequestBody @Valid Area areas, BindingResult result) {
        if(result.hasErrors()){
            return ResultUtils.error(result.getFieldError().getDefaultMessage());
        }
        areaService.insert(areas);
        return ResultUtils.success();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public Result delete(Integer id){
        return areaService.delete(id); //.deleteByKey(id);
    }

    @PutMapping
    @ApiOperation(value = "更新")
    public Result update(@RequestBody Area area) {
        return areaService.update(area);
    }

    //@GetMapping
    //public Result selectList(){
    //    return service.list(f);
    //}

    @GetMapping("/{id}")
    @ApiOperation(value = "selectDetail",notes = "查询详情")
    public Result selectDetail(@PathVariable Integer id) throws MyException {
        return ResultUtils.success(areaService.selectByKey(id));
    }


}
